using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that the single side of a Many-To-One <see cref="DMRelationship"/>
    /// has the Single side <see cref="DeleteParentAction"/> set to <see cref="DeleteParentAction.DoNothing"/>.
    /// </summary>
    public class DMRelationshipOneDirectionMustBeDoNothing : IBusinessObjectRule
    {
        private DMRelationship _dmRelationship;

//        public DMRelationshipOneDirectionMustBeDoNothing(DMRelationship dmRelationship)
//        {
//            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//            _dmRelationship = dmRelationship;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmRelationship = (DMRelationship) bo;
            return _dmRelationship.IsOneToMany || _dmRelationship.IsOneDeleteActionDoNothing;
        }


        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMRelationshipOneDirectionMustBeDoNothing"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                string reverseRelMessage = "";
                DMRelationship reverseRelationship = (DMRelationship) _dmRelationship.ReverseRelationship;
                if (reverseRelationship != null)
                {
                    reverseRelMessage = " which is a reverse of the Many Relationship '"
                                        + reverseRelationship + "' on Class '" + reverseRelationship.ClassDM + "'" ;
                }
                //return "The Single Relationship '" + _dmRelationship.ToString()
                //       + "' which is a reverse of the Many Relationship '"
                //       + reverseRelationship.ToString() + "' must be 'DoNothing'";
                return "The Single Relationship '" + _dmRelationship
                       + "' on Class '" + _dmRelationship.ClassDM + "'" + reverseRelMessage + " must be 'DoNothing'";
            }
        }

        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}